<?php

class Admin_Model_Store {

    private $_dbtable;

    public function __construct() {
        $this->_dbtable = new Admin_Model_DbTable_Store();
    }

    /**
     * 
     * @param type $enabled
     * full[DEFAULT] -> full 
     * true -> only enabled
     * false -> only disabled
     * @param type $formated
     * true -> get all details with decrypted json value, converted to array format, ready to print on page.
     * false[DEFAULT] -> returns details as a table rowset.
     * @return type $formated = true returns Array else table rowset.
     */
    public function fetchAll($enabled = "full", $formated = false) {
        $select = $this->_dbtable->select();
        switch ($enabled) {
            case "true":
                $select->where("enabled = true");
                break;
            case "false":
                $select->where("enabled = false");
                break;
            default:
                break;
        }
        $result = $this->_dbtable->fetchAll($select);
        if (!$formated) {
            return $result;
        } else {
            $allStores = array();
            foreach ($result as $storeValue) {
                array_push($allStores, $this->getStoreValuesInFormatedArray($storeValue));
            }
            return $allStores;
        }
    }

    /**
     * 
     * @param type $data
     * @return type
     */
    public function addNewStore($data) {
        return $this->_dbtable->insert($data);
    }

    /**
     * 
     * @param type $data
     * @param type $id
     * @return type
     */
    public function updateStore($data, $id) {
        return $this->_dbtable->update($data, "id = $id");
    }

    /**
     * 
     * @param type $id
     * @param $formated 
     * true -> get all details with decrypted json value, converted to array format, ready to print on page.
     * false[DEFAULT] -> returns details as a table rowset.
     * @return type $formated = true returns Array else table rowset.
     */
    public function getStoreById($id, $formated = false) {
        $select = $this->_dbtable->select();
        $select->where("id = $id");
        $result = $this->_dbtable->fetchAll($select);
        if (!$formated) {
            return $result;
        } else {
            return $this->getStoreValuesInFormatedArray($result[0]);
        }
    }

    public function getStoreValuesInFormatedArray($data) {
        #extract row data
        $detailJson = json_decode($data['detail']);
        #generae brands
        $bransArray = array();
        $brandDb = new Admin_Model_Brand();
        foreach ($detailJson->details->brands as $brandId) {
            array_push($bransArray, $brandDb->getBrandById($brandId, true));
        }
        #location
        $zipDb = new Admin_Model_Zipcode();
        $zipWithCityArray = $zipDb->getZipWithCityArray($detailJson->details->location);
        #format all data in array format.
        $storeArray = array();
        $storeArray['store_id'] = $data['id'];
        $storeArray['store_name'] = $detailJson->details->name;
        $storeArray['address1'] = $detailJson->details->address1;
        $storeArray['address2'] = $detailJson->details->address2;
        $storeArray['email'] = $detailJson->details->email;
        $storeArray['contact1'] = $detailJson->details->contact1;
        $storeArray['contact2'] = $detailJson->details->contact2;
        $storeArray['map'] = $detailJson->details->map;
        $storeArray['brands'] = $bransArray;
        $storeArray['location'] = $zipWithCityArray;
        $storeArray['store_enabled'] = $data['enabled'];
        return $storeArray;
    }

}

